const postcss = require("postcss");
var autoprefixer = require("autoprefixer");
var postcssNested = require("postcss-nested");
var tailwindcss = require("tailwindcss");
var { renderSync } = require("node-sass");
const {  getGlobalValue, replaceComponentName, staticPath, readFile, writeFile, buildConfigPath, buildAssetsPath } = require("../util");

function buildTailwindcssSass(purge, body) {
    var css = `@import "tailwindcss/base";@import "tailwindcss/components";@import "tailwindcss/utilities";` + renderSync({ data: body }).css;
    return new Promise((resolve, reject) => {
        postcss([
            tailwindcss({
                purge: purge,
                content: [],
                theme: {
                    extend: {}
                },
                plugins: [],
                corePlugins: {
                    preflight: false
                }
            }),
            autoprefixer({
                overrideBrowserslist: ["last 2 version", ">1%", "ios 7"]
            }),
            postcssNested
        ])
            .process(css, { from: "./src/app.css", to: "./dest/app.css" })
            .then((result) => {
                resolve(result.css.replace(/[\r\n]/g, ""));
            });
    });
}

/**
 * @description: 编辑js
 * @param {*} paths 加载的文件
 * @param {*} themeConfig 模版配置
 * @return {*}
 */
async function buildThemejs(paths, themeConfig) {
    var jsBody = readFile(staticPath("app.js"));
    paths.forEach((path) => {
        jsBody += replaceComponentName(readFile(path + "js"), path);
    });

    try {
        writeFile(buildAssetsPath(themeConfig.name, "js"), jsBody);

        return Promise.resolve("js写入成功");
    } catch (error) {
        console.log("css错误", error);
        return Promise.reject("错误");
    }
}

/**
 * @description: 编辑css
 * @param {*} paths 加载的文件
 * @param {*} themeConfig 模版配置
 * @return {*}
 */
async function buildThemeCss(paths, themeConfig) {
    var scssBody = readFile(staticPath("app.css"));
    var tailwindcssPurge = [];

    paths.forEach((path) => {
        scssBody += readFile(path + "scss");
        tailwindcssPurge.push(path + "liquid");
    });

    try {
        var css = await buildTailwindcssSass(tailwindcssPurge, scssBody);

        writeFile(buildAssetsPath(themeConfig.name, "css"), css);

        return Promise.resolve("css写入成功");
    } catch (error) {
        console.log("css错误", error);
        return Promise.reject("错误");
    }
}

async function createThemeGlobal(global, themeConfig) {
    try {
        var globalSetting = getGlobalValue(global);

        var settingsData = {
            global: globalSetting,
            pages_settings: [
                {
                    route: "index/index",
                    route_handle: "",
                    sections: [
                        {
                            type: "block_slides",
                            fixed: 2,
                            params: {
                                settings: {
                                    is_play: false,
                                    duration: "3",
                                    height: "big",
                                    is_width_fill: true
                                },
                                blocks: [
                                    {
                                        pc_image: {
                                            src: "",
                                            alt: ""
                                        },
                                        mobile_image: {
                                            src: "",
                                            alt: ""
                                        },
                                        link: {
                                            type: "",
                                            title: "",
                                            url: ""
                                        },
                                        block_type: "slide-item"
                                    },
                                    {
                                        pc_image: {
                                            src: "",
                                            alt: ""
                                        },
                                        mobile_image: {
                                            src: "",
                                            alt: ""
                                        },
                                        link: {
                                            type: "",
                                            title: "",
                                            url: ""
                                        },
                                        block_type: "slide-item"
                                    }
                                ]
                            }
                        },
                        {
                            type: "block_collection_list",
                            fixed: 2,
                            params: {
                                settings: {
                                    collection: {
                                        id: "",
                                        title: ""
                                    },
                                    block_title: "Featured Products",
                                    block_detail: "",
                                    product_number: "8",
                                    pc_number: "4",
                                    wap_number: "2",
                                    more: "View More"
                                },
                                blocks: []
                            }
                        },
                        {
                            type: "block_image_text",
                            fixed: 2,
                            params: {
                                settings: {
                                    is_fill: true,
                                    image: {
                                        src: "",
                                        alt: ""
                                    },
                                    img_width: 50,
                                    img_position: "flex-row",
                                    title: "Blind box",
                                    detail: "It's time to spell luck, see who is the lucky star, willing to open the heart",
                                    btn_text: "Show more",
                                    link: {
                                        type: "",
                                        title: "",
                                        url: ""
                                    },
                                    title_color: "#585858",
                                    detail_color: "#585858",
                                    btn_bg: "#ffa500",
                                    btn_color: "#fff"
                                },
                                blocks: []
                            }
                        },
                        {
                            type: "block_collection",
                            fixed: 2,
                            params: {
                                settings: {
                                    block_title: "Collection list",
                                    block_detail: "",
                                    is_fill: false,
                                    pc_number: "4",
                                    yd_number: "2",
                                    more_text: ""
                                },
                                blocks: [
                                    {
                                        collection: {
                                            id: "",
                                            title: ""
                                        },
                                        block_type: "collection-item"
                                    },
                                    {
                                        collection: {
                                            id: "",
                                            title: ""
                                        },
                                        block_type: "collection-item"
                                    },
                                    {
                                        collection: {
                                            id: "",
                                            title: ""
                                        },
                                        block_type: "collection-item"
                                    },
                                    {
                                        collection: {
                                            id: "",
                                            title: ""
                                        },
                                        block_type: "collection-item"
                                    }
                                ]
                            }
                        },
                        {
                            type: "block_newsletter",
                            fixed: 2,
                            params: {
                                settings: {
                                    is_fill: true,
                                    title: "Subscribe to our newsletter",
                                    detail: "A short sentence describing what someone will receive by subscribing",
                                    buttonName: "Subscribe",
                                    newsletterTip: "Thank you for joining our mailing list!",
                                    submit_bg: "#ffa500",
                                    submit_color: "#fff",
                                    title_color: "#212121",
                                    detail_color: "#585858",
                                    margin_bottom: 80,
                                    img_position: "none",
                                    image: {
                                        src: "",
                                        alt: ""
                                    }
                                },
                                blocks: []
                            }
                        }
                    ]
                },
                {
                    route: "product/detail",
                    route_handle: "",
                    sections: [
                        {
                            type: "product_detail",
                            fixed: 4,
                            params: {
                                settings: {
                                    is_facebook: true,
                                    is_twitter: true,
                                    is_pinterest: true,
                                    bread_crumbs: true,
                                    is_dialog: true,
                                    is_vk: true,
                                    is_tumblr: true,
                                    position: "right",
                                    is_linkedin: true,
                                    is_whatsapp: true,
                                    is_line: true,
                                    template: "2",
                                    back_btn: "show",
                                    detail_type: "tab",
                                    content: "",
                                    image_type: "bottom"
                                },
                                blocks: []
                            }
                        }
                    ]
                },
                {
                    route: "collection/detail",
                    route_handle: "",
                    sections: [
                        {
                            type: "collection_detail",
                            fixed: 4,
                            params: {
                                settings: {
                                    refresh: true,
                                    pagesize: 12,
                                    show_img: true,
                                    show_top: true,
                                    show_bottom: true,
                                    show_sort: true,
                                    show_availability: false,
                                    show_price: false,
                                    show_vendor: false
                                },
                                blocks: [
                                    {
                                        block_type: "filter-item",
                                        filter_title: "filter",
                                        filter_open: true,
                                        filter: {
                                            filter_type: 1,
                                            product_tag: []
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                },
                {
                    route: "collection/list",
                    route_handle: "",
                    sections: [
                        {
                            type: "collection_list",
                            fixed: 1,
                            params: {
                                settings: {
                                    pagesize: 12,
                                    refresh: true
                                },
                                blocks: [
                                    {
                                        collection: {
                                            id: "",
                                            title: ""
                                        },
                                        block_type: "collection-item"
                                    }
                                ]
                            }
                        }
                    ]
                },
                {
                    route: "topic/list",
                    route_handle: "",
                    sections: [
                        {
                            type: "topic_list",
                            fixed: 1,
                            params: {
                                settings: {
                                    pagesize: 12,
                                    refresh: true
                                },
                                blocks: [
                                    {
                                        topic: {
                                            id: "",
                                            title: ""
                                        },
                                        block_type: "topic-item"
                                    }
                                ]
                            }
                        }
                    ]
                },
                {
                    route: "topic/detail",
                    route_handle: "",
                    sections: []
                },
                {
                    route: "search/detail",
                    route_handle: "",
                    sections: [
                        {
                            type: "search_detail",
                            fixed: 4,
                            params: {
                                settings: {
                                    pagesize: 12,
                                    placeholder: "search",
                                    search_values: [],
                                    refresh: true
                                },
                                blocks: []
                            }
                        }
                    ]
                },
                {
                    route: "blog/list",
                    route_handle: "",
                    sections: [
                        {
                            type: "blog_list",
                            fixed: 4,
                            params: {
                                settings: {
                                    pagesize: 12,
                                    refresh: true
                                },
                                blocks: []
                            }
                        }
                    ]
                }
            ]
        };

        var settingsSchema = {
            global_setting: global,
            pages_setting: [
                {
                    name: {
                        zh_CN: "首页",
                        en_US: "Home page"
                    },
                    route: "index/index",
                    max_block: "40",
                    route_handle: "",
                    sections: [
                        {
                            type: "header",
                            fixed: "top",
                            name: {
                                zh_CN: "页头",
                                en_US: "Page header"
                            }
                        },
                        {
                            type: "block_slides",
                            fixed: "",
                            name: {
                                zh_CN: "轮播图",
                                en_US: "Slideshow"
                            }
                        },
                        {
                            type: "block_collection_list",
                            fixed: "",
                            name: {
                                zh_CN: "专辑商品",
                                en_US: "Collection"
                            }
                        },
                        {
                            type: "block_image_text",
                            fixed: "",
                            name: {
                                zh_CN: "图文",
                                en_US: "Image and text"
                            }
                        },
                        {
                            type: "block_collection",
                            fixed: "",
                            name: {
                                zh_CN: "专辑列表",
                                en_US: "Collection list"
                            }
                        },
                        {
                            type: "block_newsletter",
                            fixed: "",
                            name: {
                                zh_CN: "邮件订阅",
                                en_US: "Mail subscription"
                            }
                        },
                        {
                            type: "footer",
                            fixed: "bottom",
                            name: {
                                zh_CN: "页尾",
                                en_US: "End of page"
                            }
                        }
                    ]
                },
                {
                    name: {
                        zh_CN: "商品详情页",
                        en_US: "Product details page"
                    },
                    route: "product/detail",
                    max_block: 40,
                    route_handle: "",
                    sections: [
                        {
                            type: "header",
                            fixed: "top",
                            name: {
                                zh_CN: "页头",
                                en_US: "Page header"
                            }
                        },
                        {
                            type: "product_detail",
                            fixed: "require",
                            name: {
                                zh_CN: "商品详情",
                                en_US: "Product details"
                            }
                        },
                        {
                            type: "footer",
                            fixed: "bottom",
                            name: {
                                zh_CN: "页尾",
                                en_US: "End of page"
                            }
                        }
                    ]
                },
                {
                    name: {
                        zh_CN: "专辑详情",
                        en_US: "Collection Details"
                    },
                    route: "collection/detail",
                    max_block: 40,
                    route_handle: "",
                    sections: [
                        {
                            type: "header",
                            fixed: "top",
                            name: {
                                zh_CN: "页头",
                                en_US: "Page header"
                            }
                        },
                        {
                            type: "collection_detail",
                            fixed: "require",
                            name: {
                                zh_CN: "专辑详情",
                                en_US: "Collection Details"
                            }
                        },
                        {
                            type: "footer",
                            fixed: "bottom",
                            name: {
                                zh_CN: "页尾",
                                en_US: "End of page"
                            }
                        }
                    ]
                },
                {
                    name: {
                        zh_CN: "专辑列表",
                        en_US: "Collection list"
                    },
                    route: "collection/list",
                    max_block: 3,
                    route_handle: "",
                    sections: [
                        {
                            type: "header",
                            fixed: "top",
                            name: {
                                zh_CN: "页头",
                                en_US: "Page header"
                            }
                        },
                        {
                            type: "collection_list",
                            fixed: "top",
                            name: {
                                zh_CN: "专辑列表",
                                en_US: "Collection list"
                            }
                        },
                        {
                            type: "footer",
                            fixed: "bottom",
                            name: {
                                zh_CN: "页尾",
                                en_US: "End of page"
                            }
                        }
                    ]
                },
                {
                    name: {
                        zh_CN: "活动专题列表",
                        en_US: "Event Topic list"
                    },
                    route: "topic/list",
                    max_block: 3,
                    route_handle: "",
                    sections: [
                        {
                            type: "header",
                            fixed: "top",
                            name: {
                                zh_CN: "页头",
                                en_US: "Page header"
                            }
                        },
                        {
                            type: "topic_list",
                            fixed: "top",
                            name: {
                                zh_CN: "活动专题列表",
                                en_US: "Event Topic list"
                            }
                        },
                        {
                            type: "footer",
                            fixed: "bottom",
                            name: {
                                zh_CN: "页尾",
                                en_US: "End of page"
                            }
                        }
                    ]
                },
                {
                    name: {
                        zh_CN: "活动专题详情",
                        en_US: "Event Details"
                    },
                    route: "topic/detail",
                    max_block: 40,
                    route_handle: "",
                    sections: [
                        {
                            type: "header",
                            fixed: "top",
                            name: {
                                zh_CN: "页头",
                                en_US: "Page header"
                            }
                        },
                        {
                            type: "footer",
                            fixed: "bottom",
                            name: {
                                zh_CN: "页尾",
                                en_US: "End of page"
                            }
                        }
                    ]
                },
                {
                    name: {
                        zh_CN: "搜索页",
                        en_US: "Search page"
                    },
                    route: "search/detail",
                    max_block: 40,
                    route_handle: "",
                    sections: [
                        {
                            type: "header",
                            fixed: "top",
                            name: {
                                zh_CN: "页头",
                                en_US: "Page header"
                            }
                        },
                        {
                            type: "search_detail",
                            fixed: "require",
                            name: {
                                zh_CN: "搜索页",
                                en_US: "Search page"
                            }
                        },
                        {
                            type: "footer",
                            fixed: "bottom",
                            name: {
                                zh_CN: "页尾",
                                en_US: "End of page"
                            }
                        }
                    ]
                },
                {
                    name: {
                        zh_CN: "博客列表",
                        en_US: "Blog list"
                    },
                    route: "blog/list",
                    max_block: 40,
                    route_handle: "",
                    sections: [
                        {
                            type: "header",
                            fixed: "top",
                            name: {
                                zh_CN: "页头",
                                en_US: "Page header"
                            }
                        },
                        {
                            type: "blog_list",
                            fixed: "require",
                            name: {
                                zh_CN: "博客列表",
                                en_US: "Blog list"
                            }
                        },
                        {
                            type: "footer",
                            fixed: "bottom",
                            name: {
                                zh_CN: "页尾",
                                en_US: "End of page"
                            }
                        }
                    ]
                },
                {
                    name: {
                        zh_CN: "优惠码列表页",
                        en_US: "Coupon list page"
                    },
                    route: "coupon/list",
                    max_block: 20,
                    route_handle: "",
                    sections: [
                        {
                            type: "header",
                            fixed: "top",
                            name: {
                                zh_CN: "页头",
                                en_US: "Page header"
                            }
                        },
                        {
                            type: "coupons_list",
                            fixed: "require",
                            name: {
                                zh_CN: "优惠券列表",
                                en_US: "Coupon list"
                            }
                        },
                        {
                            type: "footer",
                            fixed: "bottom",
                            name: {
                                zh_CN: "页尾",
                                en_US: "End of page"
                            }
                        }
                    ]
                },
                {
                    name: {
                        zh_CN: "优惠码详情页",
                        en_US: "Coupon detail page"
                    },
                    route: "coupon/detail",
                    max_block: 20,
                    route_handle: "",
                    sections: [
                        {
                            type: "header",
                            fixed: "top",
                            name: {
                                zh_CN: "页头",
                                en_US: "Page header"
                            }
                        },
                        {
                            type: "coupons_detail",
                            fixed: "require",
                            name: {
                                zh_CN: "优惠券详情",
                                en_US: "Coupon details"
                            }
                        },
                        {
                            type: "footer",
                            fixed: "bottom",
                            name: {
                                zh_CN: "页尾",
                                en_US: "End of page"
                            }
                        }
                    ]
                },
                {
                    name: {
                        zh_CN: "满减活动列表页",
                        en_US: "Promotion list page"
                    },
                    route: "promotion/list",
                    max_block: 20,
                    route_handle: "",
                    sections: [
                        {
                            type: "header",
                            fixed: "top",
                            name: {
                                zh_CN: "页头",
                                en_US: "Page header"
                            }
                        },
                        {
                            type: "promotion_list",
                            fixed: "require",
                            name: {
                                zh_CN: "活动列表",
                                en_US: "Promotion list"
                            }
                        },
                        {
                            type: "footer",
                            fixed: "bottom",
                            name: {
                                zh_CN: "页尾",
                                en_US: "End of page"
                            }
                        }
                    ]
                },
                {
                    name: {
                        zh_CN: "满减活动详情页",
                        en_US: "Promotion detail page"
                    },
                    route: "promotion/detail",
                    max_block: 20,
                    route_handle: "",
                    sections: [
                        {
                            type: "header",
                            fixed: "top",
                            name: {
                                zh_CN: "页头",
                                en_US: "Page header"
                            }
                        },
                        {
                            type: "promotion_detail",
                            fixed: "require",
                            name: {
                                zh_CN: "活动详情",
                                en_US: "Promotion detail"
                            }
                        },
                        {
                            type: "footer",
                            fixed: "bottom",
                            name: {
                                zh_CN: "页尾",
                                en_US: "End of page"
                            }
                        }
                    ]
                }
            ],
            blocks_setting: [
                {
                    type: "card_header",
                    name: {
                        en_US: "Basic Blocks",
                        zh_CN: "基础积木"
                    },
                    sections: []
                },
                {
                    type: "card_header",
                    name: {
                        en_US: "Product Blocks",
                        zh_CN: "商品块"
                    },
                    sections: []
                },
                {
                    type: "card_header",
                    name: {
                        en_US: "Operation Blocks",
                        zh_CN: "运营类"
                    },
                    sections: []
                }
            ],
            default_blocks_setting: {
                mode: "",
                exclude_block_ids: ["", ""]
            }
        };
        writeFile(buildConfigPath(themeConfig.name, "settings_data.json"), JSON.stringify(settingsData, "", "\t"));
        writeFile(buildConfigPath(themeConfig.name, "settings_schema.json"), JSON.stringify(settingsSchema, "", "\t"));

        return Promise.resolve("config写入成功");
    } catch (error) {
        console.log("css错误", error);
        return Promise.reject("config写入错误");
    }
}

module.exports = { buildTailwindcssSass, buildThemejs, buildThemeCss, createThemeGlobal };
